package com.qf.controller.manager;

import com.github.pagehelper.PageInfo;
import com.qf.entitys.Business;
import com.qf.entitys.CommodityClassification;
import com.qf.entitys.MenuItem;
import com.qf.entitys.RespBean;
import com.qf.service.MenuItemService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/manager/menuitem")
public class MenuItemController {

    @Resource
    private MenuItemService service;

    @GetMapping("/list")
    public String list(Integer pageindex, MenuItem item, Business business, CommodityClassification classification,HttpServletRequest request){
//        if (bname!=null&&bname.length()!=0){
//            Business business = new Business();
//            business.setBname(bname);
//            item.setBusiness(business);
//        }
//        if (ccName!=null&&ccName.length()!=0){
//            CommodityClassification classification = new CommodityClassification();
//            classification.setCcName(ccName);
//            item.setClassification(classification);
//        }
        item.setBusiness(business);
        item.setClassification(classification);
        System.out.println(item);
        PageInfo<MenuItem> menuItemPageInfo = service.selectAll(pageindex,item);
        request.setAttribute("pageInfo",menuItemPageInfo);
        request.setAttribute("bname",business.getBname());
        request.setAttribute("mname",item.getMname());
        request.setAttribute("ccName",classification.getCcName());
        request.setAttribute("putShelves",item.getPutShelves());
        return "manager/MenuitemList";
    }

    /**
     * 跳转审核商品页面
     * @param pageindex
     * @param item
     * @param business
     * @param classification
     * @param request
     * @return
     */
    @GetMapping("/putfalse")
    public String toputfalse(Integer pageindex, MenuItem item, Business business, CommodityClassification classification,HttpServletRequest request){
        item.setBusiness(business);
        item.setPutShelves(false);
        item.setClassification(classification);
        System.out.println(item);
        PageInfo<MenuItem> menuItemPageInfo = service.selectAll(pageindex,item);
        request.setAttribute("pageInfo",menuItemPageInfo);
        request.setAttribute("bname",business.getBname());
        request.setAttribute("mname",item.getMname());
        request.setAttribute("ccName",classification.getCcName());
        return "manager/MenuitemListFalse";
    }

    /**
     * 跳转已审核商品页面
     * @param pageindex
     * @param item
     * @param business
     * @param classification
     * @param request
     * @return
     */
    @GetMapping("/puttrue")
    public String toputture(Integer pageindex, MenuItem item, Business business, CommodityClassification classification,HttpServletRequest request){
        item.setBusiness(business);
        item.setPutShelves(true);
        item.setClassification(classification);
        System.out.println(item);
        PageInfo<MenuItem> menuItemPageInfo = service.selectAll(pageindex,item);
        request.setAttribute("pageInfo",menuItemPageInfo);
        request.setAttribute("bname",business.getBname());
        request.setAttribute("mname",item.getMname());
        request.setAttribute("ccName",classification.getCcName());
        return "manager/MenuitemListTrue";
    }

    /**
     * 可以进行商品状态的修改，1为启用，0为停用
     * @param
     * @return
     */
    @GetMapping("/updateMenu")
    @ResponseBody
    public RespBean updateMenu(Integer mid,Boolean putShelves){
        MenuItem menuItem = new MenuItem();
        menuItem.setMid(mid);
        menuItem.setPutShelves(putShelves);
        int i = service.updateByPrimaryKeySelective(menuItem);
        if (i>0){
            return RespBean.success("操作成功!");
        }else {
            return RespBean.error("操作失败!");
        }
    }

    @PostMapping("/updateMenu")
    @ResponseBody
    public RespBean updateMenus(@RequestParam("ids[]")Integer[] ids,Integer putShelves){
        try {
            int i = service.updatePutByMids(ids, putShelves);
            if (i>0){
                return RespBean.success("批量操作成功！");
            }else {
                return RespBean.error("批量操作失败！");
            }
        }catch (Exception ex){
            return RespBean.error("批量操作失败！");
        }
    }

    /**
     * 拒绝一个商品
     * @param mid
     * @return
     */
    @GetMapping("/delMenu")
    @ResponseBody
    public RespBean delMenu(Integer mid){
        try {
            int i = service.deleteByPrimaryKey(mid);
            if (i>0){
                return RespBean.success("拒绝成功！");
            }else {
                return RespBean.error("拒绝失败！");
            }
        }catch (Exception ex){
            return RespBean.error("拒绝失败！");
        }
    }

    /**
     * 批量拒绝商品
     */
    @GetMapping("/delbyids")
    @ResponseBody
    public RespBean delbyids(@RequestParam("ids[]")Integer[] ids){
        try {
            int i = service.delbyids(ids);
            if (i>0){
                return RespBean.success("批量拒绝成功！");
            }else {
                return RespBean.error("批量拒绝失败！");
            }
        }catch (Exception ex){
            return RespBean.error("批量拒绝失败！");
        }
    }


}
